<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: HCZ <huangchengze@etrol.com> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\Admin;
use app\admin\controller\api\User as UserApi;

/**
 * 用户管理
 * @package app\admin
 */
class User  extends Admin

{
	
	protected $api;
	
	public function _initialize(){
		parent::_initialize();
		$this->api = new UserApi();
	}
    /**
     * 用户列表 
     * @return mixed
     */
   public function index() {
   	if(IS_AJAX){
   		return $this->api->UserList();
   	}
   	$model = model('User');
   	$userPropertyList = $model->userPropertyList;
    $this->assign('userPropertyList',json_encode($userPropertyList, JSON_UNESCAPED_UNICODE));
    return $this->fetch();
 	}
 	
 	/**
 	 * 添加用户
 	 * @return View/Json
 	 */
 	public function add(){
 		if(IS_POST){
 			$data = input('post.');
 			//密码盐值
 			$data['salt'] = rand_string(6);
 			//初始化密码
 			if(!isset($data['password'])||$data['password']==""){
 				$data['password'] = '123456';
 			}
 			//有效性
 			if(isset($data['validity'])){
 				$data['validity'] = 1;
 			}else{
 				$data['validity'] = 0;
 			}
 			return $this->api->add($data);
 		}
 		$model = model('User');
 		//角色选项
 		$roleList = $this->api->getRole()->getData();
 		//所属单位选项
 		$enterpriseList = $this->api->getEnterprise()->getData();
 		//用户属性选项
 		$userPropertyList = $model->userPropertyList;
 		$this->assign('roleList',$roleList['data']);
 		$this->assign('enterpriseList',$enterpriseList['data']);
 		$this->assign('userPropertyList',$userPropertyList);
 		return $this->fetch();
 	}
 	
 	/**
 	 * 编辑用户
 	 * @param string $userId
 	 * @return View/Json
 	 */
 	public function edit($userId = null){
 		if(IS_POST){
 			$data = input('post.');
 			//密码为空,不更改密码
 			if($data['password']==""){
 				unset($data['password']);
 			}else{
 				$data['salt'] = rand_string(6);
 			}
 			//有效性
 			if(isset($data['validity'])){
 				$data['validity'] = 1;
 			}else{
 				$data['validity'] = 0;
 			}
 			return $this->api->edit($data,$userId);
 		}
 		$model = model('User');
 		//角色选项
 		$roleList = $this->api->getRole()->getData();
 		$this->assign('roleList',$roleList['data']);
 		//所属单位选项
 		$enterpriseList = $this->api->getEnterprise()->getData();
 		$this->assign('enterpriseList',$enterpriseList['data']);
 		//用户属性选项
 		$userPropertyList = $model->userPropertyList;
 		$this->assign('userPropertyList',$userPropertyList);
 		//用户信息
 		$user = model('User')->find($userId);
 		if($roles = $user->roles){
 			$user['role'] = $roles[0]['id'];
 		}else{
 			$user['role'] = '';
 		}
 		$this->assign('info',$user);
 		return $this->fetch();
 		
 	}
 	
 	/**
 	 * 删除用户
 	 * @return Json
 	 */
 	public function del(){
 		if(IS_GET){
 			return $this->api->del(input('userId'));
 		}
 	}
 	
 	/**
 	 * 搜索用户
 	 * @return Json
 	 */
 	public function search() {
 		if(IS_AJAX){
 			$userList = $this->api->search(input('keyword'));
 				
 			return $userList;
 		}
 	}
 

}